package com.scoy.honeymei.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.mall.BookActivity;
import com.scoy.honeymei.activity.mall.MallMeActivity;
import com.scoy.honeymei.activity.mall.TravelMeActivity;
import com.scoy.honeymei.activity.mall.TripMeActivity;
import com.scoy.honeymei.activity.me.AddressListActivity;
import com.scoy.honeymei.activity.me.ApplyActivity;
import com.scoy.honeymei.activity.me.ContactActivity;
import com.scoy.honeymei.activity.me.MeCouponActivity;
import com.scoy.honeymei.activity.me.MeVipActivity;
import com.scoy.honeymei.activity.me.PersonActivity;
import com.scoy.honeymei.activity.me.SystemActivity;
import com.scoy.honeymei.activity.me.WalletActivity;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.PersonBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @ClassName: MeFragment$
 * @Description: 个人中心
 * @Author: GaoZhan
 * @CreateDate: 2019/8/30/0030$ 上午 9:21$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/30/0030$ 上午 9:21$
 * @UpdateRemark: 更新内容
 */
public class MeFragment extends BaseFragment {

    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.me_photo_civ)
    CircleImageView mePhotoCiv;
    @BindView(R.id.me_name_tv)
    TextView meNameTv;
    @BindView(R.id.me_sign_tv)
    TextView meSignTv;
    @BindView(R.id.me_toperson_iv)
    ImageView meTopersonIv;
    @BindView(R.id.me_toaddress_tv)
    TextView meToaddressTv;
    @BindView(R.id.me_toticket_tv)
    TextView meToticketTv;
    @BindView(R.id.me_tovip_tv)
    TextView meTovipTv;
    @BindView(R.id.me_toreplay_tv)
    TextView meToreplayTv;
    @BindView(R.id.me_towallet_tv)
    TextView meTowalletTv;
    @BindView(R.id.me_toservice_tv)
    TextView meToserviceTv;
    @BindView(R.id.me_toset_tv)
    TextView meTosetTv;
    @BindView(R.id.me_tomp_tv)
    LinearLayout meTompTv;

    @BindView(R.id.mf_film_llt)
    LinearLayout mfFilmLlt;
    @BindView(R.id.mf_travel_llt)
    LinearLayout mfTravelLlt;
    @BindView(R.id.mf_trip_llt)
    LinearLayout mfTripLlt;
    @BindView(R.id.mf_shop_llt)
    LinearLayout mfShopLlt;

    @BindView(R.id.mf_film_tv)
    TextView mfFilmTv;
    @BindView(R.id.mf_travel_tv)
    TextView mfTravelTv;
    @BindView(R.id.mf_trip_tv)
    TextView mfTripTv;
    @BindView(R.id.mf_shop_tv)
    TextView mfShopTv;

    private Unbinder unbinder;
    private Context mContext;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();

        initNormal();

        httpPerson();

        return view;
    }

    @Override
    public void initNormal() {

        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

    }

    @OnClick({R.id.me_toperson_iv, R.id.me_toaddress_tv, R.id.me_toticket_tv, R.id.me_tovip_tv,
            R.id.me_toreplay_tv, R.id.me_towallet_tv, R.id.me_toservice_tv, R.id.me_toset_tv, R.id.me_tomp_tv,
            R.id.mf_film_llt, R.id.mf_travel_llt, R.id.mf_trip_llt, R.id.mf_shop_llt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.me_tomp_tv:
            case R.id.me_toperson_iv:
                startActivityForResult(new Intent(mContext, PersonActivity.class), 0x7);
                break;
            case R.id.mf_film_llt:
                startActivity(new Intent(mContext, BookActivity.class));
                break;
            case R.id.mf_travel_llt:
                startActivity(new Intent(mContext, TravelMeActivity.class));
                break;
            case R.id.mf_trip_llt:
                startActivity(new Intent(mContext, TripMeActivity.class));
                break;
            case R.id.mf_shop_llt:
                startActivity(new Intent(mContext, MallMeActivity.class));
                break;

            case R.id.me_toaddress_tv:
                startActivity(new Intent(mContext, AddressListActivity.class));
                break;
            case R.id.me_toticket_tv:
                startActivity(new Intent(mContext, MeCouponActivity.class));
//                startSaoYiSao();
                break;
            case R.id.me_tovip_tv:
                startActivity(new Intent(mContext, MeVipActivity.class));
                break;
            case R.id.me_toreplay_tv:
                startActivity(new Intent(mContext, ApplyActivity.class));
                break;
            case R.id.me_towallet_tv:
                startActivity(new Intent(mContext, WalletActivity.class));
                break;
            case R.id.me_toservice_tv:
                startActivity(new Intent(mContext, ContactActivity.class));
                break;
            case R.id.me_toset_tv:
                startActivityForResult(new Intent(mContext, SystemActivity.class), 0x88);
                break;
        }
    }


    private void startSaoYiSao(){
        Intent intent = new Intent(getActivity(), CaptureActivity.class);
        ZxingConfig config = new ZxingConfig();
        config.setShowbottomLayout(true);//底部布局（包括闪光灯和相册）
        config.setPlayBeep(true);//是否播放提示音
        config.setShake(true);//是否震动
        config.setShowAlbum(true);//是否显示相册
        config.setShowFlashLight(true);//是否显示闪光灯
        intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
        startActivityForResult(intent, 10);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x787:
                httpPerson();
                break;
            case 10:
                // 扫描二维码/条码回传
                if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
                    if (data != null) {
                        String content = data.getStringExtra(Constant.CODED_CONTENT);
//                        result.setText("扫描结果为：" + content);
                    }
                }
                break;
        }
    }

    //获取个人信息
    private void httpPerson() {
        HttpParams params = new HttpParams();
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.PERSON_DATE, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                PersonBean personBean = new Gson().fromJson((String) o, PersonBean.class);
                GlideImgUtil.glidePic(mContext, personBean.getImage(), mePhotoCiv);
                meNameTv.setText("".endsWith(personBean.getName()) ? "无名" : personBean.getName());
                int type = personBean.getType();//"0个人 1商户"
                if (type == 1) {
                    meSignTv.setText("商户");
                } else {
                    meSignTv.setText("个人");
                }

                mfFilmTv.setText(personBean.getMovie_order_num() + "");
                mfTravelTv.setText(personBean.getLvxing_order_num() + "");
                mfTripTv.setText(personBean.getChuxing_order_num() + "");
                mfShopTv.setText(personBean.getGoods_order_num() + "");

                int fileSawInt = personBean.getMovie_guide_status();
                //0=未观看,1=已观看
                SPHelper.applyInt(MeConstant.STATE_SHOW, fileSawInt);

            }

        });

    }


}
